package solution

import "math"

func V2(nums []int, target int) int {
	if len(nums) == 0 {
		return 0
	}

	if target > nums[len(nums)-1] {
		return len(nums)
	}

	from := 0

	to := len(nums) - 1

	for true {
		index := from + int(math.Floor(float64(to-from)/2))

		if index >= 1 && target > nums[index-1] && target <= nums[index] {
			return index
		}

		if target <= nums[index] {
			if index == 0 {
				return index
			}

			to = index - 1
		} else {
			if index == len(nums)-1 {
				return len(nums)
			}

			from = index + 1
		}
	}

	return len(nums)
}
